﻿using cpe.cpein.UI.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.Windows.Threading;

namespace cpe.cpein.UI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class FMainWindow : Window
    {
        #region Variables declaration

        /// <summary>
        /// Current user.
        /// </summary>
        UserCpeInServiceReference.User _currentUser;

        #region UserControls

        /// <summary>
        /// List Group UC.
        /// </summary>
        UCListGroups ucGroupsList;

        /// <summary>
        /// List Group UC.
        /// </summary>
        UCListUsers ucUsersList;

        /// <summary>
        /// User UC
        /// </summary>
        UCUser ucUser;

        /// <summary>
        /// News UC
        /// </summary>
        UCNews ucNews;

        /// <summary>
        /// Potential contact UC
        /// </summary>
        UCPotentialContact ucPotentialContact;

        #endregion

        #endregion

        #region Create and Open

        /// <summary>
        /// Default constructor.
        /// </summary>
        public FMainWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Constructor with parameters
        /// </summary>
        /// <param name="usr"></param>
        public FMainWindow(UserCpeInServiceReference.User usr)
        {
            InitializeComponent();
            this._currentUser = usr;
        }

        /// <summary>
        /// Fires when the window is loaded.
        /// </summary>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            InitializeWindow();
        }

        /// <summary>
        /// Used to initialize the window.
        /// </summary>
        private void InitializeWindow()
        {
            // Set Header informations
            FillHeaderInfo();
            ucPotentialContact = new UCPotentialContact(this._currentUser);
            LoadUserControlSide(ucPotentialContact);
            // Put the dashboard UC as main UC 
            ucNews = new UCNews(this._currentUser);
            LoadUserControl(ucNews);
        }

        #endregion

        #region Events Methods

        /// <summary>
        /// Fired when the profile's button is clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnProfil_Click(object sender, RoutedEventArgs e)
        {
            this.ucUser = new UCUser(this._currentUser, this._currentUser);
            this.LoadUserControl(this.ucUser);
        }

        private void btnHome_Click(object sender, RoutedEventArgs e)
        {
            if (ucNews == null)
                ucNews = new UCNews();
            LoadUserControl(ucNews);
        }

        /// <summary>
        /// Fires when the form is closing.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Closing(object sender, CancelEventArgs e)
        {
            CloseApplication(e);
        }

        /// <summary>
        /// Fires when th button Groups is clicked.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void btnGroups_Click(object sender, EventArgs args)
        {
            if (ucGroupsList == null)
                ucGroupsList = new UCListGroups(this._currentUser, this);
            LoadUserControl(ucGroupsList);
        }

        private void btnNews_Click(object sender, RoutedEventArgs e)
        {
            if (ucNews == null)
                ucNews = new UCNews(this._currentUser);
            LoadUserControl(ucNews);
        }

        private void btnContacts_Click(object sender, RoutedEventArgs e)
        {
            if (ucUsersList == null)
                ucUsersList = new UCListUsers(this._currentUser, this);
            LoadUserControl(ucUsersList);
        }

        #endregion

        #region Methods

        /// <summary>
        /// Method used to close the application.
        /// </summary>
        private void CloseApplication(CancelEventArgs e)
        {
            if (System.Windows.MessageBox.Show("Voulez-vous vraiment quitter l'application ?", "Fermeture de l'application", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
            {
                // Disconnect the current user. 
                DisconnectCurrentUser();

                // Close the app
                Application.Current.Shutdown();
            }
            else
                e.Cancel = true;
        }

        /// <summary>
        /// Method used to disconnect the current user.
        /// </summary>
        private void DisconnectCurrentUser()
        { 
            using(UserCpeInServiceReference.UsersServiceClient us = new UserCpeInServiceReference.UsersServiceClient())
            {
                us.DisconnectUser(this._currentUser, this._currentUser);
            }
        }

        /// <summary>
        /// Function used to Fill the form header informations.
        /// </summary>
        private void FillHeaderInfo()
        {
            if (this._currentUser != null)
            {
                this.lblUserName.Text = this._currentUser.Name.Trim() + " " + this._currentUser.Lastname.Trim();
                this.lblUserMail.Text = this._currentUser.Email.Trim();
            }
            else
            {
                this.lblUserName.Text = "";
                this.lblUserMail.Text = "";
            }
        }

        /// <summary>
        /// Function which allows to load a UserControl in the form.
        /// </summary>
        /// <param name="uc"></param>
        public void LoadUserControl(UserControl uc)
        {
            this.ucContent.Content = uc;
        }

        /*private void loadMessageList()
        {
            if (_currentUser != null)
            {
                Tools.Tools.BeginWaitStatus();
                List<Message> messages = (from m in _userService.GetNewsFeed(this._currentUser, this._currentUser)
                                          orderby m.Date descending
                                          select m).ToList();
                MessageGrid.ItemsSource = messages;
                Tools.Tools.EndWaitStatus();
            }
        }*/

        /// <summary>
        /// Function which allows to load a UserControl in the form.
        /// </summary>
        /// <param name="uc"></param>
        public void LoadUserControlSide(UserControl uc)
        {
            this.ucSide.Content = uc;
        }

        #endregion

    }
}
